import com.jfinal.core.Controller;
import com.jfinal.json.FastJson;
import com.jfinal.log.Log;

import java.text.DecimalFormat;
import java.util.Map;

/**
 * @authc yaowk
 * 2017/6/16
 */
public class IndexController extends Controller {

    public void index() {
        render("index.html");
    }

    public void notifyUrl() {
        Log log = Log.getLog("");
        log.info("notifyUrl:");
        log.info(FastJson.getJson().toJson(getParaMap()));
        renderJson("success");
    }

    public void callbackUrl() {
        Log log = Log.getLog("");
        log.info("callbackUrl:");
        log.info(FastJson.getJson().toJson(getParaMap()));
        render("success.html");
    }

    public void pay() {
        Integer payType = getParaToInt("payType");
        Double money;
        try {
            money = Double.valueOf(getPara("money"));
            if (money <= 0) {
                renderJson("金额不能低于0元");
                return;
            }
        }
        catch (Exception e) {
            renderJson("金额只能为数字");
            return;
        }
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        PayService payService = new PayService();
        Map<String, String> result = payService.sendPay(decimalFormat.format(money), payType);
        if ("0".equals(result.get("status")) && "0".equals(result.get("result_code"))) {
            renderQrCode(result.get("pay_info"), 200, 200);
        }
        else {
            render("error.html");
        }
    }

    public void orderQuery() {
        PayService payService = new PayService();
        Map result = payService.orderQuery();
        renderJson(result);
    }

}
